<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card>
        <!-- el-form :inlin 一行显示 -->
        <el-form inline :model="formSearch" ref="formInline">
          <el-row type="flex"  justify="space-between"  style="min-width:500px">

            <el-form-item label="关键字">
              <el-input
                v-model="formSearch.keyword"
                placeholder="根据文章标题搜索"
              ></el-input>
            </el-form-item>

            <el-form-item >
              <el-button @click="onClear" size="small">清除</el-button>
              <el-button type="primary" @click="onSearch" size="small">搜索</el-button>
            </el-form-item>

          </el-row>
        </el-form>
        <!-- 警示语 -->
        <el-alert
          type="info"
          show-icon
          :closable="false"
          class="aleart"
          :title="'数据一共' + total + '条'"
        />
        <!-- 表格 -->
        <el-table
          v-loading="loading"
          :data="tableData"
          style="width: 100%">
          <el-table-column
            width="190px"
            prop="id"
            label="编号">
          </el-table-column>
          <el-table-column
            max-width="80px"
            label="题型">
            <template #default="{row:{questionType}}">
              {{diaQuestionType(questionType)}}
            </template>
          </el-table-column>
          <el-table-column
            min-width="175px"
            prop="questionIDs"
            label="题目编号">
            <template #default="{ row:{questionIDs} }">
              <el-row v-for="item in questionIDs" :key="item.number"><el-button type="text" @click="openQuetion(item)">{{item.number}}</el-button></el-row>
            </template>
          </el-table-column>
          <el-table-column
            min-width="150px"
            prop="addTime"
            label="录入时间">
          </el-table-column>
          <el-table-column
            prop="totalSeconds"
            label="答题时间(S)">
          </el-table-column>
          <el-table-column
            prop="accuracyRate"
            label="正确率(%)">
          </el-table-column>
          <el-table-column
            prop="userName"
            label="收录人">
          </el-table-column>
          <el-table-column
            label="操作">
            <template #default="{ row }">
              <el-button type="danger" icon="el-icon-delete" circle plain @click="onRemove(row)"></el-button>
            </template>
          </el-table-column>
        </el-table>

        <!-- 分页区域 -->
        <div style="height: 60px; margin-top: 16px">
          <el-pagination
            :current-page="formSearch.page"
            :page-sizes="[10, 20, 30, 50 ]"
            :page-size="formSearch.pagesize"
            layout="->, prev, pager, next,sizes,total,jumper"
            :total="total"
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
          />
        </div>
      </el-card>
    </div>

    <!-- 题目预览的dailog -->
    <!-- 预览的dialog -->
    <el-dialog
    @closed="particulars = {}"
    title="题目预览"
    :visible.sync="dialogVisible"
    width="55%"
    :before-close="handleClose">
    <div>
      <el-row>
        <el-col :span="6"><div class="grid-content bg-purple" >【题型】：{{diaQuestionType(particulars.questionType)}}</div></el-col>
        <el-col :span="6"><div class="grid-content bg-purple-light">【编号】：{{particulars.id}}</div></el-col>
        <el-col :span="6"><div class="grid-content bg-purple"></div>【难度】：{{diadifficulty(particulars.difficulty)}}</el-col>
        <el-col :span="6"><div class="grid-content bg-purple-light">【标签】：{{particulars.tags}}</div></el-col>
      </el-row>
      <el-row>
        <el-col :span="6"><div class="grid-content bg-purple">【学科】：{{particulars.subjectName}}</div></el-col>
        <el-col :span="6"><div class="grid-content bg-purple-light">【目录】：{{particulars.directoryName}}</div></el-col>
        <el-col :span="6"><div class="grid-content bg-purple"></div>【方向】：{{particulars.direction}}</el-col>
      </el-row>
    </div>
    <el-divider></el-divider>
    <div>
      <p>【题干】：</p>
      <p v-html="particulars.question"></p>
      <p>{{particulars.questionType}} 选项：（以下选中的选项为正确答案）</p>
      <p v-if="particulars.questionType==='1'">
        <el-row v-for="item in particulars.options" :key="item.id" style="height:52px;width:300px;line-height: 50px">
          <el-radio
          style="display:inline-block"
          label-width="200px"
          :label="item.title"
          :value="item.isRight===1?item.title:''"
          ></el-radio>
          <img
          style="height:50px;vertical-align: middle;float:right"
          v-if="item.img"
          :src="item.img" alt=""
          @click="magnify(img.img)" >
        </el-row>
      </p>
      <p v-if="particulars.questionType==='2'">
        <el-row v-for="k in particulars.options" :key="k.id" style="height:52px;width:300px;line-height: 50px">
          <el-checkbox
          style="display:inline-block"
          label-width="200px"
          :label="k.title"
          :value="k.isRight===1?true:false"
          ></el-checkbox>
          <img
          v-if="k.img"
          style="height:50px;vertical-align: middle;float:right"
          :src="k.img" alt=""
          @click="magnify(k.img)" >
        </el-row>
      </p>
    </div>
    <el-divider></el-divider>
    <div>
      <p>【参考答案】：<el-button type="danger" size="small" v-model="isState" @click="isState=true">视频答案预览</el-button></p>
        <video width="420" height="200" controls autoplay v-if="isState">
          <source src="../../assets/MP4/myy.mp4" >
          <object data="movie.mp4" width="320" height="240">
            <!-- <embed width="420" height="200" src="particulars.videoURL"> -->
          </object>
        </video>
    </div>
    <el-divider></el-divider>
    <p >【答案解析】：<span v-html="particulars.answer"></span> </p>
    <el-divider></el-divider>
    <p>【题目备注】：{{particulars.remarks}}</p>
    <span slot="footer" class="dialog-footer">
      <el-button type="primary" @click="handleClose" >关闭</el-button>
    </span>
    </el-dialog>

  </div>
</template>

<script>
import { randoms, removeRandoms, detail } from '@/api/hmmm/questions'
import { questionType, difficulty } from '@/api/hmmm/constants'
export default {
  name: 'Articles',
  computed: {
  },
  data () {
    return {
      isState: false, // 视频窗口的状态
      dialogVisible: false, // dailog的状态
      particulars: {}, // 文章详情
      total: 180,
      formSearch: {
        keyword: '', // 关键字
        page: 1, // 当前页
        pagesize: 10 // 每页条数
      },
      tableData: [],
      loading: false,
      dialogMagnify: false, // 放大图片
      img: '' // 图片地址
    }
  },
  created () {
    this.getRandoms()
  },
  methods: {
    async getRandoms () { // 获取题组数据
      this.loading = true
      const { data } = await randoms(this.formSearch)
      // console.log(data)
      this.total = data.counts
      this.tableData = data.items
      this.formSearch.page = ~~data.page
      this.formSearch.pagesize = ~~data.pagesize
      this.loading = false
    },
    handleSizeChange (size) { // 每页条数变化
      this.formSearch.pagesize = size
      this.formSearch.page = 1
      this.getRandoms()
    },
    handleCurrentChange (page) { // 当前页变化
      this.formSearch.page = page
      this.getRandoms()
    },
    onClear () { // 清除按钮
      this.formSearch.keyword = ''
    },
    onSearch () { // 搜索按钮
      this.getRandoms()
      this.onClear()
    },
    onRemove (obj) { // 删除按钮
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
        await removeRandoms(obj) // 删除题组
        this.$message({
          type: 'success',
          message: '删除成功!'
        })
        this.getRandoms()
      }).catch(_ => _)
    },
    async openQuetion (item) { // 点击题目编号查看题目
      // console.log(item)
      try {
        const { data } = await detail(item)
        this.particulars = data
        this.dialogVisible = true
      } catch {
        this.$message.warning('后台的数据，好像出了点问题')
      }
    },
    handleClose () { // dailog关闭按钮
      this.dialogVisible = false
      this.isState = false
    },

    // 放大图片
    magnify (img) {
      this.img = img
      this.dialogMagnify = true
    },

    //  关闭放大图片
    handleCloseMagnify () {
      this.dialogMagnify = false
    },

    diaQuestionType (k) { // 格式化题型
      const res = questionType.find(item => item.value === ~~k)
      return res ? res.label : '未知'
    },
    diadifficulty (k) { // 格式化难度
      const res = difficulty.find(item => item.value === ~~k)
      return res ? res.label : '未知'
    }
  }
}
</script>

<style scoped lang="scss">
.app-container {
  ::v-deep h2 {
    margin: 0 0 10px;
    padding: 0;
  }
  .text {
    color: #606266;
    font-weight: bold;
    margin: 0px 10px 0px 20px;
  }
  .search {
    margin-right: 40px;
  }
  .clear {
    margin: 0px 0px 0px 10px;
  }
  .table {
    margin-top: 20px;
  }
  // 页脚样式
  ::v-deep .btn-prev,
  .btn-next {
    margin: 0 5px;
    background-color: #f4f4f5;
    color: #606266;
    min-width: 30px;
    border-radius: 2px;
  }
  ::v-deep .btn-next {
    margin: 0 5px;
    background-color: #f4f4f5;
    color: #606266;
    min-width: 30px;
    border-radius: 2px;
  }
  ::v-deep .el-pager li:not(.disabled).active {
    margin: 0 5px;
    background-color: #409eff;
    color: #fff;
    min-width: 30px;
    border-radius: 2px;
  }
  // 页脚
  element.style {
    text-align: right !important;
  }
}
</style>
<style >
  .el-menu--horizontal{
    z-index: 9999999999999999999999 !important;
  }
</style>
